package com.briup.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean//自己创建模板对象放到IOC容器
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        System.out.println("测试");
        //1.创建对象
        RedisTemplate redisTemplate = new RedisTemplate();
        //2.设置连接信息
        redisTemplate.setConnectionFactory(factory);
        //3.修改默认key值的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //4.修改默认的value值的序列化器 json格式字符串
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(new ObjectMapper()));
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        //字符串序列化器：直接将字符串以字节信息返回UTF-8
        //5.设置hash key的序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //6．设置hash value序列化方法
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(new ObjectMapper()));
        return redisTemplate;
    }
}
